﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using AliyunMobileTestTechnology.ViewModel.Commands;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml;
using System.Collections.ObjectModel;

namespace AliyunMobileTestTechnology.ViewModel
{
    public class WorkPlatformViewModel : INotifyPropertyChanged
    {
        public ICommand StartCollectCov
        {
            get { return new StartCollectCovCommand(this); }
        }

        private bool listViewIsEnable = true;
        private RepositoriesViewModel repositories = new RepositoriesViewModel();

        private string svnCommitPath = "none";
        public string SvnCommitPath
        {
            set
            {
                svnCommitPath = value;
                PropertyChangedEventHandler handle = PropertyChanged;
                if (handle != null)
                {
                    handle.Invoke(this, new PropertyChangedEventArgs("SvnCommitPath"));
                }
            }
            get
            {
                return svnCommitPath;
            }
        }
       
        public ICommand StopCollectCov
        {
            get { return new StopCollectCovCommand(this); }
        }
        public ICommand SvnCommitChanges
        {
            get { return repositories.SvnCommitChanges; }
        }
        public List<string> PackageNameList;

        public string TestCaseName;

        public bool ListViewIsEnable
        {
            set
            {
                listViewIsEnable = value;
                PropertyChangedEventHandler handle = PropertyChanged;
                if (handle != null)
                {
                    handle.Invoke(this, new PropertyChangedEventArgs("ListViewIsEnable"));
                }
            }
            get 
            {
                return listViewIsEnable;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<Module> Modules
        {
            get
            {
                ObservableCollection<Module> modules = new ObservableCollection<Module>();
                XmlDocument doc = new XmlDocument();
                doc.Load("TestModule.xml");

                XmlNodeList moduleNodes = doc.SelectNodes("/Modules/Module");

                foreach (XmlNode node in moduleNodes)
                {
                    Module module = new Module();
                    module.Name = node.Attributes["Name"].Value;

                    XmlNodeList APPs = node.SelectNodes("APPs/Name");

                    foreach (XmlNode appNode in APPs)
                    {
                        module.APPs.Add(appNode.InnerText);
                    }

                    XmlNodeList packages = node.SelectNodes("Packages/Name");

                    foreach (XmlNode packageNode in packages)
                    {
                        module.Packages.Add(packageNode.InnerText);
                    }
                    modules.Add(module);
                }

                return modules;
            }
        }
        
        public static string ExecuteCommand(string commandText, string workingDirectory = "")
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            if (workingDirectory != "")
            {
                p.StartInfo.WorkingDirectory = workingDirectory;
            }
            p.StartInfo.Arguments = "/c " + commandText;
            string strOutput = null;
            try
            {
                p.Start();
                //StreamWriter utf8Writer = new StreamWriter(p.StandardInput.BaseStream, Encoding.UTF8);
                //utf8Writer.WriteLine(commandText);
                //utf8Writer.WriteLine("exit");
                //utf8Writer.Close();
                //byte[] buffer = System.Text.Encoding.Default.GetBytes(commandText);
                //p.StandardInput.BaseStream.Write(buffer, 0, buffer.Length);
                //p.StandardInput.WriteLine();
                //p.StandardInput.WriteLine("exit");
                strOutput = p.StandardOutput.ReadToEnd();

                p.WaitForExit();
                p.Close();
            }
            catch (Exception e)
            {
                strOutput = e.Message;
            }
            return strOutput;
        }
    }

    public class Module
    {
        private string name;
        public string Name
        {
            get { return name;}
            set { name = value;}
        }
        public List<string> Packages =  new List<string>();
        public List<string> APPs = new List<string>();
    }
}
